package com.scse.bms.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;


/**
 * @Title: SendEmailService
 * @Author: 吴赠禹
 * @Date: 2021/2/26 23:13
 * @Version: 1.0
 * @Description: 邮件发送服务
 */

@Slf4j
@Service
public class SendEmailService {

    @Autowired
    private JavaMailSender mailSender;

    /**
     * 读取配置项属性值并注入
     */
    @Value("${spring.mail.username}")
    private String from;

    /**
     * 发送纯文本邮件
     *
     * @param to      收件人
     * @param subject 主题
     * @param text    文本内容
     */
    public void sendSimpleEmail(String to, String subject, String text) {
        // 定制纯文本邮件信息SimpleMailMessage
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);
        message.setTo(to);
        message.setSubject(subject);
        message.setText(text);
        try {
            // 发送邮件
            mailSender.send(message);
            log.info("[sendSimpleEmail] 纯文本邮件发送成功！");
        } catch (MailException e) {
            log.info("[sendSimpleEmail] 纯文本邮件未能发送！" + e.getMessage());
        }
    }


    /**
     * 发送复杂邮件（包括静态资源和附件）
     *
     * @param to
     * @param subject
     * @param text
     * @param filePath
     * @param rscId
     * @param rscPath
     */
    public void sendComplexEmail(String to, String subject, String text,
                                 String filePath, String rscId, String rscPath) {
        // 定制复杂邮件信息MimeMessage
        MimeMessage message = mailSender.createMimeMessage();
        try {
            // 使用MimeMessageHepler帮助类，并设置multiparty多部件使用为true
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            // 开始HTML模式
            helper.setText(text, true);
            // 设置静态资源 TODO
            FileSystemResource resource = new FileSystemResource(new File(rscPath));
            helper.addInline(rscId, resource);
            // 设置邮件附件
            FileSystemResource file = new FileSystemResource(new File(filePath));
            // 截取最后一个文件分隔符后的字串作为附件名
            String fileName = filePath.substring(filePath.lastIndexOf(File.separator));
            helper.addAttachment(fileName, file);
            // 发送邮件
            mailSender.send(message);
            log.info("[sendComplexEmail] 复杂邮件发送成功！");
        } catch (MessagingException e) {
            log.info("[sendComplexEmail] 复杂邮件发送失败！" + e.getMessage());
        }

    }


    /**
     * 发送模板邮件
     *
     * @param to
     * @param subject
     * @param content
     */
    public void sendTemplateEmail(String to, String subject, String content) {
        MimeMessage message = mailSender.createMimeMessage();
        try {
            // 使用MimeMessageHelper帮助类，并设置multipart多部件使用为true
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content, true);
            // 发送邮件
            mailSender.send(message);
            log.info("[sendTemplateEmail] 模板邮件发送成功！");
        } catch (MessagingException e) {
            log.info("[sendTemplateEmail] 模板邮件发送失败！" + e.getMessage());
        }

    }


    /**
     * 只发送附件和文本内容
     */
    public void sendAttachmentMail(String to, String subject, String text, String filePath) {

        MimeMessage message = mailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(text, true);
            // 设置附件
            FileSystemResource resource = new FileSystemResource(new File(filePath));
            // 设置附件名
            String fileName = filePath.substring(filePath.lastIndexOf(File.separator));
            helper.addAttachment(fileName, resource);
            // 发送只有附件的邮件
            mailSender.send(message);
            log.info("[sendAttachmentEmail] 发送附件邮件成功");
        } catch (MessagingException e) {
            log.info("[sendAttachmentEmail] 附送附件邮件失败" + e.getMessage());
        }


    }


}
